#include <iostream>
#include <algorithm>
using namespace std;
int a[]={1,0,0,0,1,0,1,0,2,1};
const int N=2e5+10;
typedef long long LL;
LL n;
LL b[N];
int count(int x)
{
	int s=0;
	while(x)
	{
		int p=x%10;
		s+=a[p];
		x/=10;
	}
	return s;
}
bool cmp(int x,int y)
{
	int j=count(x);
	int k=count(y); 
	if(j!=k)return j<k;
	else return x<y;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>b[i];
	}
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
	{
		cout<<b[i]<<" ";
	}
	return 0;
}
